var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    youhuinum: 0.00,
    paynum: 0.00,
    zhekou: 10,
    xiaofei: 0,
    shopinfo: {},
    latitude: '',
    longitude: '',
    shopid: '',
    yuanjia: 0,
    placeholder: '请输入消费金额',
    value: '',
    leitab:1,
    itemtab:2,
    oil_info:'',
    shop_price:0,    //店铺价
    national_price:0,//国际价
    system_price:0, //系统价
    cut1:0,     //比国际价低
    cut2:0,        //比站内价低
    xiaofei_num:0,
    di1:true,
    di2:false,
    bindtel:false,
    hasit:false,
    mhiden:false,
    now_page:1,
    total_page:0,
    commentList:[],
    hasit92:false,
    hasit95:false,
    hasit0:false,
    isCanPay:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    if (options.scene) {
      var scene = decodeURIComponent(options.scene);
      var shopstr = scene.split('?');
      var shoparr = scene.split('=');
      var shopid = shoparr[1];
      this.setData({
        shopid: shopid
      })
      
      this.checkmobile();


    } else {
      var shopid = options.id;
      var leitab = 0;
      var itemtab = 0;
      if (options.type){
        var type = options.type;
        itemtab = type;
        if (type == 4) {
          leitab = 2;
        } else {
          leitab = 1;
        }
      }else{
        leitab = 1;
        itemtab = 2;
      }
      
      this.setData({
        shopid: shopid,
        itemtab:itemtab,
        leitab:leitab
      });
    }
    this.checkmobile();
    this.getaddress(shopid);
    this.getComment()
  },
  previewBanner(e){
    let index = e.currentTarget.dataset.index;
    let mentou = this.data.shopinfo.mentou;
    wx.previewImage({
      current: mentou[index], // 当前显示图片的http链接
      urls: mentou // 需要预览的图片http链接列表
    })
  },
  // 获取评价列表
  getComment(now_page = 1){
    app.post({
      url: "api/shopr/oilCommentList",
      data: {
        shop_id: this.data.shopid,
        now_page:now_page
      },
      success:(e)=> {
        console.log(e)
        if (e.data.code == 1) {
          let res = e.data.data;
          this.setData({
            commentList:this.data.commentList.concat(res.list),
            total_page:res.total_page
          })
        }
      }
    });
  },
  previewImage(e){
    console.log(e)
    let index = e.currentTarget.dataset.index;
    let url = e.currentTarget.dataset.url;
    let commentList = this.data.commentList;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: commentList[index].imgs // 需要预览的图片http链接列表
    })
  },
  // 检测是否绑定手机号
  checkmobile:function(){
    var status = wx.getStorageSync('status');
    status = parseInt(status);
    if(status == 0){
      app.post({
        url:"api/userr/judgePhone",
        data:{
          user_id:wx.getStorageSync('userId')
        },
        success:res=>{
          if(res.data.data.need){
            this.setData({
              bindtel:true
            })
          }
        }
      })
     
    }
  },
  bindtel:function(){
      wx.navigateTo({
        url: '/pages/mine/mobile/mobile?type=10&shopid='+this.data.shopid,
      })
  },
  // 获取当前位置
  getaddress: function (shopid) {
    var that = this;
    var latitude = wx.getStorageSync('latitude');
    var longitude = wx.getStorageSync('longitude');
    if(latitude){
      that.setData({
        latitude: latitude,
        longitude: longitude
      })
      that.getshop(shopid, longitude, latitude);
    }else{
      // 获取当前地理位置
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          that.setData({
            latitude: latitude,
            longitude: longitude
          })
          that.getshop(shopid, longitude, latitude);
        }
      })
    }
  },

  leiselect:function(e){
    var inx = e.currentTarget.dataset.index;
    var itemtab = 0;
    var shop_price = 0;
    var national_price = 0;
    var system_price = 0;
    var cut1 = 0;
    var cut2 = 0;
    var di1 = false;
    var di2 = false;
    if(inx == 1){
      itemtab = 2;
      shop_price = this.data.oil_info.qiyou['#92'].shop_oil;
      national_price = this.data.oil_info.qiyou['#92'].national_price;
      system_price = this.data.oil_info.qiyou['#92'].system_price;
      cut1 = this.data.oil_info.qiyou['#92'].cut1;
      cut2 = this.data.oil_info.qiyou['#92'].cut2;
    }
    if(inx == 2){
      itemtab =4;
      shop_price = this.data.oil_info.qiyou['#0'].shop_oil;
      national_price = this.data.oil_info.qiyou['#0'].national_price;
      system_price = this.data.oil_info.qiyou['#0'].system_price;
      cut1 = this.data.oil_info.qiyou['#0'].cut1;
      cut2 = this.data.oil_info.qiyou['#0'].cut2;
    }
    if (cut1 >= 0) {
      di1 = true
    }
    if (cut2 >= 0) {
      di2 = true
    }
    cut1 = Math.abs(cut1).toFixed(2);
    cut2 = Math.abs(cut2).toFixed(2);
    this.setData({
      leitab:inx,
      itemtab:itemtab,
      shop_price:shop_price,
      national_price:national_price,
      system_price: system_price,
      cut1:cut1,
      di1:di1,
      cut2:cut2,
      di2:di2
    })
    this.get_paynum();
  },

  itemselect:function(e){
    var inx = e.currentTarget.dataset.index;
    this.get_shopprice(inx);
    this.get_paynum();
  },
  get_shopprice:function(inx){
    var shop_price = 0;
    var national_price = 0;
    var system_price = 0;
    var jiang_num = 0;
    var di_num = 0;
    var cut1 = 0;
    var cut2 = 0;
    var di1 = false;
    var di2 = false;

    if (inx == 1) {
      shop_price = this.data.oil_info.qiyou['#98'].shop_oil;
      national_price = this.data.oil_info.qiyou['#98'].national_price;
      system_price = this.data.oil_info.qiyou['#98'].system_price;
      cut1 = this.data.oil_info.qiyou['#98'].cut1;
      cut2 = this.data.oil_info.qiyou['#98'].cut2;
    }
    if (inx == 2) {
      shop_price = this.data.oil_info.qiyou['#92'].shop_oil;
      national_price = this.data.oil_info.qiyou['#92'].national_price;
      system_price = this.data.oil_info.qiyou['#92'].system_price;
      cut1 = this.data.oil_info.qiyou['#92'].cut1;
      cut2 = this.data.oil_info.qiyou['#92'].cut2;
    }
    if (inx == 3) {
      shop_price = this.data.oil_info.qiyou['#95'].shop_oil;
      national_price = this.data.oil_info.qiyou['#95'].national_price;
      system_price = this.data.oil_info.qiyou['#95'].system_price;
      cut1 = this.data.oil_info.qiyou['#95'].cut1;
      cut2 = this.data.oil_info.qiyou['#95'].cut2;
    }
    if (inx == 4) {
      shop_price = this.data.oil_info.qiyou['#0'].shop_oil;
      national_price = this.data.oil_info.qiyou['#0'].national_price;
      system_price = this.data.oil_info.qiyou['#0'].system_price;
      cut1 = this.data.oil_info.qiyou['#0'].cut1;
      cut2 = this.data.oil_info.qiyou['#0'].cut2;
    }
    if (cut1 > 0 || cut1 == 0) {
      di1 = true
    }
    if (cut2 > 0 || cut2 == 0) {
      di2 = true
    }
    cut1 = Math.abs(cut1).toFixed(2);
    cut2 = Math.abs(cut2).toFixed(2);
    this.setData({
      itemtab: inx,
      shop_price: shop_price,
      national_price: national_price,
      system_price: system_price,
      jiang_num: jiang_num,
      di_num: di_num,
      di1: di1,
      cut1: cut1,
      di2: di2,
      cut2: cut2
    })
  },
  // 获取商家信息
  getshop: function (id, longitude, latitude) {
    var that = this;
    var param = {
      url: "api/shopr/oilShopInfo",
      data: {
        shop_id: id,
        longitude: longitude,
        latitude: latitude
      },
      success: function (e) {
        var a = e.data.data.oil_info.qiyou['#98']
        if (a.shop_oil==0){
          that.setData({
            mhiden: true
          })
        }
        // 98号有价格
        if (e.data.code == 1) {
          if (e.data.data.oil_info.qiyou['#98']){
              that.setData({
                hasit:true
              })
          }
          if (e.data.data.oil_info.qiyou['#92'].shop_oil != 0) {
            that.setData({
              hasit92: true
            })
          }
          if (e.data.data.oil_info.qiyou['#95'].shop_oil != 0) {
            that.setData({
              hasit95: true
            })
          }
          if (e.data.data.oil_info.qiyou['#0'].shop_oil != 0) {
            that.setData({
              hasit0: true
            })
          }
          that.setData({
            shopinfo: e.data.data.shop_info,
            oil_info: e.data.data.oil_info,
          })
        }
        
        var itemtab = that.data.itemtab;
        that.get_shopprice(itemtab);

      }
    }
    app.post(param);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.checkmobile();
  },
  onReachBottom: function () {
    let now_page = this.data.now_page;
    console.log(now_page,this.data.total_page)
    if(now_page < this.data.total_page){
      now_page++;
      this.setData({
        now_page:now_page
      })
      this.getComment(now_page)
    }
},
  // 用户输入消费金额
  xiaofei: function (e) {
    var num = e.detail.value;
    this.setData({
      xiaofei: 1,
      xiaofei_num:num
    })
    this.get_paynum();
  },

  // 支付
  payit: function (e) {
    if(!this.data.isCanPay) return;
    console.log(e)
    var userid = wx.getStorageSync('userId');
    var shopid = this.data.shopid;
    var paynum = this.data.paynum;
    var shuru = this.data.xiaofei_num;
    var system_price = this.data.system_price;
    var shop_price = this.data.shop_price;
    var item_type = this.data.itemtab;
    var that = this;
 
    var param = {
      url: "api/shopr/payOilMoney",
      data: {
        user_id: userid,
        shop_id: shopid,
        original_price: shuru,
        system_price: system_price,
        price: paynum,
        shop_price:shop_price,
        oil_type:item_type
      },
      success: function (e) {
        console.log(e);
        if (e.data.code == 1) {
          var nonceStr = e.data.data.pay_data.nonceStr;
          var packagenum = e.data.data.pay_data.package;
          var paySign = e.data.data.pay_data.paySign;
          var signType = e.data.data.pay_data.signType;
          var timeStamp = e.data.data.pay_data.timeStamp;
          var order_id = e.data.data.order_id;
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            package: packagenum,
            signType: signType,
            paySign: paySign,
            success(res) {
              that.successpay(order_id)
            },
            fail(res) {

            }
          })
        }
      }
    }
    if(that.data.shopinfo.far){
      wx.showModal({
        cancelColor: '#999',
        cancelText:'继续支付',
        confirmColor:'red',
        confirmText:'重新选择',
        title:'温馨提醒',
        content:`您当前位置离该加油站过远\n请确认是否继续支付`,
        success:(res)=>{
          if(res.confirm){
            console.log(res.cancel)
            wx.navigateBack({
              delta:1
            })
          }else{
            this.setData({
              isCanPay:false
            })
            app.post(param);
          }
        }
      })
    }else{
      this.setData({
        isCanPay:false
      })
      app.post(param);
    }

  },
  // 支付成功
  successpay: function (id) {
    var that = this;
    var param = {
      url: "api/shopr/payOilSuccess",
      data: {
        order_id: id
      },
      success: function (e) {
        wx.showToast({
          title: e.data.msg,
          icon: "none",
          duration: 2000
        })
        that.setData({
          isCanPay:true
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/mine/otherOrderDetail/index?id='+id,
          })
        }, 500)
        that.setData({
          xiaofei_num: 0,
          paynum: 0,
          value: ''
        })
      }
    };
    app.post(param);
  },
  showdaohang: function () {
    var detail = this.data.shopinfo;
    console.log(detail)
    wx.openLocation({
      latitude: parseFloat(detail.latitude),
      longitude: parseFloat(detail.longitude),
      scale: 18,
      name: detail.shop_name,
      address: detail.address
    })
  },
  get_paynum:function(){
    var that = this;
    if(that.data.xiaofei_num){
        var param ={
          url:'api/shopr/checkPrice',
          data:{
            shop_id:that.data.shopid,
            price: that.data.xiaofei_num,
            oil_type: that.data.itemtab
          },
          success:function(res){
              that.setData({
                paynum: res.data.data.actual_price
              })
          }
        }
        app.post(param);
    }
  }
})